package design.proxy.copycglib;

/**
 * @author GY
 * @date 2019年1月27日
 * @说明:模拟Cglib代理生成的子类代理类代码
 */
public class KidProxy extends Kid {
    private IMethodInterceptor mi;
    public KidProxy(IMethodInterceptor mi) {
        this.mi = mi;
    }
    @Override
    public void eat(Food food) {
        try {
            mi.intercept(this,
                    Kid.class.getMethod("eat", new Class[]{Food.class}),
                    new Object[]{food}, this.getClass().getMethod("eatProxy",
                            new Class[]{Food.class}));
        } catch (NoSuchMethodException | SecurityException e) {
            e.printStackTrace();
        }
    }
    public void eatProxy(Food food) {
        super.eat(food);
    }
    
    @Override
    public void brushTeeth() {
        try {
            mi.intercept(this, Kid.class.getMethod("brushTeeth", new Class[]{}),
                    new Object[]{}, this.getClass().getMethod("brushTeethProxy",
                            new Class[]{}));
        } catch (NoSuchMethodException | SecurityException e) {
            e.printStackTrace();
        }
    }
    public void brushTeethProxy() {
        super.brushTeeth();
    }
    
    @Override
    public void washHand() {
        try {
            mi.intercept(this, Kid.class.getMethod("washHand", new Class[]{}),
                    new Object[]{},
                    this.getClass().getMethod("washHandProxy", new Class[]{}));
        } catch (NoSuchMethodException | SecurityException e) {
            e.printStackTrace();
        }
    }
    public void washHandProxy() {
        super.washHand();
    }
}
